# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=avogadrolibs
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-qt6")
pkgver=1.100.0
pkgrel=1
pkgdesc='Libraries that provide 3D rendering, visualization, analysis and data processing useful in computational chemistry, molecular modeling, bioinformatics, materials science, and related areas (mingw-w64)'
arch=(any)
mingw_arch=(ucrt64 clang64 clangarm64)
url='https://www.openchemistry.org/'
msys2_repository_url="https://github.com/OpenChemistry/avogadrolibs"
msys2_references=(
  'archlinux: avogadrolibs'
)
license=(spdx:BSD-3-Clause)
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "${MINGW_PACKAGE_PREFIX}-hdf5"
             #"${MINGW_PACKAGE_PREFIX}-genxrdpattern"
             "${MINGW_PACKAGE_PREFIX}-glew"
             "${MINGW_PACKAGE_PREFIX}-libarchive"
             #"${MINGW_PACKAGE_PREFIX}-libmsym"
             "${MINGW_PACKAGE_PREFIX}-mmtf-cpp"
             "${MINGW_PACKAGE_PREFIX}-nlohmann-json"
             "${MINGW_PACKAGE_PREFIX}-pybind11"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-qt6-base"
             "${MINGW_PACKAGE_PREFIX}-qt6-svg"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools"
             "${MINGW_PACKAGE_PREFIX}-spglib"
             "${MINGW_PACKAGE_PREFIX}-vtk"
             "${MINGW_PACKAGE_PREFIX}-fast_float"
             "${MINGW_PACKAGE_PREFIX}-utf8cpp")
source=(https://github.com/OpenChemistry/avogadrolibs/archive/${pkgver}/${_realname}-${pkgver}.tar.gz
        https://github.com/OpenChemistry/crystals/archive/${pkgver}/crystals-${pkgver}.tar.gz
        https://github.com/OpenChemistry/fragments/archive/${pkgver}/fragments-${pkgver}.tar.gz
        https://github.com/OpenChemistry/molecules/archive/${pkgver}/molecules-${pkgver}.tar.gz
        003-fix-install-python-modules.patch
        004-find-Qt6LinguistTools.patch
        005-fix-cmake-config-file.patch)
sha256sums=('dd836d7287776a08673e664923960d13d222c5d630d1653bd182606e644de23c'
            '5d05e49c24d435a6e9920379aa1c04ddc848b451c6cdfaa3095bd51f30a701b7'
            '847276fbc7ea0f1db76949969af9d389b6c2520c823fb82e231760b285944e21'
            '5417a2dae8e3173505ea6d3c61df2d38a2b8eec6bfa630d30fc98889c8c7ff59'
            'a47e551f39ea1cff22dcea5c6bf5b9c3aba5f69038351794c171e26638f314e7'
            '91cd109edf7b5344f5934d5d3c342f2ac20c86be54e591934df2c9c4def05067'
            '38f452b157ed6f89f0ffc584066abd14fe62c75ad452970d3cdcfdc4aba390a9')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  for _dir in crystals fragments molecules; do
    mv ${_dir}-${pkgver} ${_realname}-${pkgver}/${_dir}
  done

  cd ${_realname}-${pkgver}
  apply_patch_with_msg \
    003-fix-install-python-modules.patch \
    004-find-Qt6LinguistTools.patch \
    005-fix-cmake-config-file.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CXXFLAGS+=" -D_USE_MATH_DEFINES -Wno-ignored-attributes" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    "${_extra_config[@]}" \
    -DENABLE_TRANSLATIONS=ON \
    -DUSE_HDF5=ON \
    -DUSE_LIBARCHIVE=ON \
    -DUSE_LIBMSYM=ON \
    -DUSE_MMTF=ON \
    -DUSE_PYTHON=ON \
    -DUSE_QT=ON \
    -DUSE_SPGLIB=ON \
    -DUSE_VTK=ON \
    -DUSE_EXTERNAL_NLOHMANN=ON \
    -DUSE_EXTERNAL_PUGIXML=ON \
    -DBUILD_STATIC_PLUGINS=OFF \
    -DQT_VERSION=6 \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -B build-${MSYSTEM} \
    -S ${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build build-${MSYSTEM}
}

package_avogadrolibs() {
  depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
           "${MINGW_PACKAGE_PREFIX}-glew"
           "${MINGW_PACKAGE_PREFIX}-hdf5"
           "${MINGW_PACKAGE_PREFIX}-molequeue"
           "${MINGW_PACKAGE_PREFIX}-pugixml"
           "${MINGW_PACKAGE_PREFIX}-spglib")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python: Python bindings"
              "${MINGW_PACKAGE_PREFIX}-avogadrolibs-qt6: For the VTK and Qt plugins")

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}

  rm -r "${pkgdir}"${MINGW_PREFIX}/bin/libAvogadroQt* \
        "${pkgdir}"${MINGW_PREFIX}/lib/libAvogadroQt* \
        "${pkgdir}"${MINGW_PREFIX}/bin/libAvogadroMoleQueue* \
        "${pkgdir}"${MINGW_PREFIX}/lib/libAvogadroMoleQueue* \
        "${pkgdir}"${MINGW_PREFIX}/bin/libAvogadroVtk* \
        "${pkgdir}"${MINGW_PREFIX}/lib/libAvogadroVtk* \
        "${pkgdir}"${MINGW_PREFIX}/include/avogadro/{molequeue,qt*,vtk} \
        "${pkgdir}"${MINGW_PREFIX}/lib/avogadro2/{scripts,plugins}

  for _dir in crystals fragments molecules; do
    cp -r ${_realname}-${pkgver}/${_dir} "${pkgdir}"${MINGW_PREFIX}/share/avogadro2/
  done

  install -Dm644 ${_realname}-${pkgver}/LICENSE -t "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}
}

package_avogadrolibs-qt6() {
  pkgdesc="Qt 5 modules for Avogadro (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-avogadrolibs"
           "${MINGW_PACKAGE_PREFIX}-gcc-libs"
           "${MINGW_PACKAGE_PREFIX}-glew"
           "${MINGW_PACKAGE_PREFIX}-libarchive"
           #"${MINGW_PACKAGE_PREFIX}-libmsym"
           "${MINGW_PACKAGE_PREFIX}-qt6-base"
           "${MINGW_PACKAGE_PREFIX}-qt6-svg"
           "${MINGW_PACKAGE_PREFIX}-vtk")

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}/avogadro/molequeue
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}/avogadro/qtgui
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}/avogadro/qtopengl
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}/avogadro/qtplugins
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}/avogadro/vtk

  for _dir in crystals fragments molecules; do
    rm -rf "${pkgdir}"${MINGW_PREFIX}/share/avogadro2/${_dir}
  done

  install -Dm644 ${_realname}-${pkgver}/LICENSE -t "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}-qt6
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
